<template>
  <section class="add-address">
    <nav-bar>
      <div class="center" slot="center">添加地址</div>
    </nav-bar>
    <div class="contacts">
      <span class="left">联系人</span>
      <div class="right">
        <input type="text" placeholder="你的名字" v-model="name" />
        <p>
          <span @click="handleSwitchGender(0)">
            <svg
              class="svg"
              :style="{
                backgroundColor: currentIndex === 0 ? '#4cd964' : '#cccccc',
              }"
            >
              <use xlink:href="#check-mark" />
            </svg>
            先生</span
          >
          <span @click="handleSwitchGender(1)">
            <svg
              class="svg"
              :style="{
                backgroundColor: currentIndex === 1 ? '#4cd964' : '#cccccc',
              }"
            >
              <use xlink:href="#check-mark" /></svg
            >女士</span
          >
        </p>
      </div>
    </div>
    <div class="contact-number">
      <span class="left">联系电话</span>
      <div class="right">
        <p class="top">
          <input
            type="text"
            placeholder="你的手机号"
            :style="{ borderBottom: isShow ? '1px solid #eaeaea' : '0' }"
            v-model="phone"
          />
          <span class="plus" @click="handleShowSpare">+</span>
        </p>
        <input
          type="text"
          placeholder="备用电话"
          class="spare"
          v-show="isShow"
          v-model="sparePhone"
        />
      </div>
    </div>
    <div class="delivery-address">
      <span class="left">送餐地址</span>
      <div class="right">
        <div @click="handleSkipSearchAddress">
          <p v-if="address">{{ address }}</p>
          <p v-else>小区 / 写字楼 / 学校等</p>
        </div>
        <input
          type="text"
          placeholder="详细地址（如门牌号等）"
          v-model="detailAddress"
        />
      </div>
    </div>
    <div class="tag">
      <span class="left">标签</span>
      <input
        type="text"
        placeholder="无/家/学校/公司"
        class="right"
        v-model="tag"
      />
    </div>
    <input type="button" value="确定" @click="handleAddAddress" />
    <router-view />
  </section>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";

import { mapGetters } from "vuex";
export default {
  data() {
    return {
      currentIndex: 0,
      isShow: false,
      name: "",
      phone: "",
      sparePhone: "",
      address: "",
      detailAddress: "",
      tag: "",
      sex: 1
    };
  },
  computed: {
    ...mapGetters(["geohash"]),
  },
  mounted() {
    this.$bus.$on("handleNewAddress", (item) => {
      this.address = item.name;
    });
  },
  methods: {
    handleSwitchGender(index) {
     this.sex = index ? 2 : 1
     console.log(this.sex);
      this.currentIndex = index;
    },
    handleShowSpare() {
      this.isShow = true;
    },
    handleSkipSearchAddress() {
      this.$router.push("/confirmOrder/chooseAddress/addAddress/searchAddress");
    },
    handleAddAddress() {
      if (localStorage.getItem("user_id")) {
        let tips = "";
        if (!this.name) {
          tips = "姓名错误";
        } else if (!this.phone) {
          tips = "手机号码错误";
        } else if (!this.address) {
          tips = "送餐地址错误";
        } else if (!this.detailAddress) {
          tips = "送餐详细地址错误";
        } else if (!this.tag) {
          tips = "标签错误";
        } else {
          let tag_type = 0;
          switch (this.tag) {
            case "无":
              tag_type = 1;
              break;
            case "家":
              tag_type = 2;
              break;
            case "学校":
              tag_type = 3;
              break;
            default:
              tag_type = 4;
              break;
          }
          const data = {
            address: this.address,
            address_detail: this.detailAddress,
            geohash: this.geohash,
            name: this.name,
            phone: this.phone,
            phone_bk: this.sparePhone,
            poi_type: 0,
            sex: this.sex,
            tag: this.tag,
            tag_type
          };
          this.$bus.$emit("addAddress",data);
          this.$router.go(-1)
          return;
        }
        this.$toast.handleTipsText(tips);
      } else {
        tips = "请登录";
        this.$toast.handleTipsText("请登录");
      }
    },
  },
  components: {
    NavBar,
  },
};
</script>
<style lang="less" scoped>
@import url("assets/css/mixin");
.add-address {
  background-color: #f5f5f5;
  .initial-children();
  .contacts,
  .contact-number,
  .delivery-address,
  .tag {
    display: flex;
    justify-content: space-between;
    padding: 20px 20px 0;
    background-color: #fff;
    color: #333333;
    .left {
      width: 100px;
      border-bottom: 1px solid #eaeaea;
    }
    .right {
      border-bottom: 1px solid #eaeaea;
      flex: 1;
      input {
        width: 100%;
        padding-bottom: 15px;
        border-bottom: 1px solid #eaeaea;
      }
    }
  }
  .contacts {
    .right {
      p {
        padding: 20px 0;
        .svg {
          position: relative;
          top: -2px;
          width: 20px;
          height: 20px;
          padding: 2px;
          margin-right: 5px;
          border-radius: 50%;
          vertical-align: middle;
        }
        span {
          margin-right: 20px;
        }
      }
    }
  }
  .contact-number {
    .right {
      .top {
        position: relative;
        .plus {
          position: absolute;
          top: -40%;
          right: 0;
          font-size: 40px;
          color: #189aec;
        }
      }
      .spare {
        padding-top: 20px;
        border: 0;
      }
    }
  }
  .delivery-address {
    .right {
      color: #999999;
      p {
        padding-bottom: 20px;
        border-bottom: 1px solid #eaeaea;
      }
      input {
        padding: 20px 0;
        border: 0 !important;
        color: #999999;
      }
    }
  }
  .tag {
    input {
      padding-bottom: 20px;
      color: #999999;
    }
  }
  input[type="button"] {
   .determine();
   margin: 20px auto;
  }
}
</style>